import {ActionContext} from 'vuex';
import ListModule from '@/store/modules/list-module';
import ListState from '@/store/modules/list-state';
import User from '@/store/entities/administration/user/user';
import Ajax from '@/lib/ajax';
import PageResult from '@/store/entities/page-result';

interface UserState extends ListState<User> {
    userForEdit: any,
    allRoles: any,
    excelFile: any,
    profilePicture: string;
    permissionForEdit:any;
    pagedUsers:any,
}

class UserModule extends ListModule<UserState, any, User> {
    state = {
        totalCount: 0,
        currentPage: 1,
        pageSize: 10,
        list: new Array<User>(),
        loading: false,
        userForEdit: {},
        excelFile: {},
        profilePicture: '',
        permissionForEdit:{},
        pagedUsers:{},
    };
    actions = {
        async obtainUsers(context: ActionContext<UserState, any>, payload: any) {
            context.state.loading = true;
            const response = await Ajax.post('/api/services/app/User/ObtainUsers', payload.data);
            context.state.loading = false;
            const page = response.data.result as PageResult<User>;
            context.state.totalCount = page.totalCount;
            context.state.list = page.items;
        },
        async createOrUpdateUser(context: ActionContext<UserState, any>, payload: any) {
            await Ajax.post('/api/services/app/User/CreateOrUpdateUser', payload.data);
        },
        async deleteUser(context: ActionContext<UserState, any>, payload: any) {
            await Ajax.delete('/api/services/app/User/DeleteUser?Id=' + payload.data.id);
        },
        async getUserForEdit(context: ActionContext<UserState, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/User/GetUserForEdit?Id=' + payload.data.id);
            context.state.userForEdit = response.data.result;
        },
        async unlockUser(context: ActionContext<UserState, any>, payload: any) {
            await Ajax.post('/api/services/app/User/UnlockUser', payload.data);
        },
        async resetPassword(context: ActionContext<UserState, any>, payload: any) {
            await Ajax.post('/api/services/app/User/ResetPassword', payload.data);
        },
        async obtainUsersToExcel(context: ActionContext<UserState, any>, payload: any) {
            const response = await Ajax.post('/api/services/app/User/ObtainUsersToExcel',payload.data);
            context.state.excelFile = response.data.result;
        },

        async getUserPermissionsForEdit(context:ActionContext<UserState, any>,payload:any){
          const response=await Ajax.get('/api/services/app/User/GetUserPermissionsForEdit?Id='+payload.data.id);
          context.state.permissionForEdit=response.data.result;
        },

        async resetUserSpecificPermissions(context:ActionContext<UserState, any>,payload:any){
            await Ajax.post('/api/services/app/User/ResetUserSpecificPermissions',payload.data);
        },

        async updateUserPermissions(context:ActionContext<UserState, any>,payload:any){
          await Ajax.put('/api/services/app/User/UpdateUserPermissions',payload.data);
        },

        async obtainPagedUsers(context:ActionContext<UserState, any>,payload:any){
            context.state.loading=true;
            const response = await Ajax.post('/api/services/app/User/ObtainUsers', payload.data);
            context.state.loading=false;
            context.state.pagedUsers=response.data.result;
        },

        async getProfilePictureByUser(context: ActionContext<UserState, any>, payload: any) {
            const response = await Ajax.get('/api/services/app/Profile/GetProfilePictureByUser?Id='+payload.data.id);
            context.state.profilePicture = response.data.result.profilePicture;
        },
    };
    mutations = {
        setCurrentPage(state: UserState, page: number) {
            state.currentPage = page;
        },
        setPageSize(state: UserState, pageSize: number) {
            state.pageSize = pageSize;
        },
    };
}

const userModule = new UserModule();
export default userModule;
